from django.db import models
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from mdeditor.fields import MDTextField


class DocCompanyName(models.Model):
    name = models.CharField(_('公司名称'), max_length=200, null=True, unique=True)
    code = models.CharField(_('文件编号'), max_length=200, null=True, unique=True)

    objects = models.Manager()

    class Meta:
        ordering = ['id']
        verbose_name = _('公司名称')
        verbose_name_plural = _('公司名称')

    def __str__(self):
        return "{}-{}".format(self.code, self.name)


class DocDepartmentName(models.Model):
    name = models.CharField(_('部门名称'), max_length=200, null=True, unique=True)
    code = models.CharField(_('文件编号'), max_length=200, null=True, unique=True)

    objects = models.Manager()

    class Meta:
        ordering = ['id']
        verbose_name = _('部门名称')
        verbose_name_plural = _('部门名称')

    def __str__(self):
        return "{}-{}".format(self.code, self.name)


class DocumentLevel(models.Model):
    level_code = models.PositiveSmallIntegerField(_('等级编号'), unique=True)

    objects = models.Manager()

    class Meta:
        ordering = ['id']
        verbose_name = _('文件等级')
        verbose_name_plural = _('文件等级')

    def __str__(self):
        return "{0}".format(self.level_code)


class VersionCode(models.Model):
    code = models.CharField(_('版本编号'), max_length=200, null=True, unique=True)

    objects = models.Manager()

    class Meta:
        ordering = ['code']
        verbose_name = _('版本编号')
        verbose_name_plural = _('版本编号')

    def __str__(self):
        return "{0}".format(self.code)


class DocType(models.Model):
    code = models.CharField(_('文件类型编号'), max_length=200, null=True, unique=True)
    name = models.CharField(_('文件类型'), max_length=200, null=True, unique=True)

    objects = models.Manager()

    class Meta:
        ordering = ['id']
        verbose_name = _('文件类型')
        verbose_name_plural = _('文件类型')

    def __str__(self):
        return "{}-{}".format(self.code, self.name)


class SopDocument(models.Model):
    name = models.CharField(_('文件名称'), max_length=200, null=True)
    code = models.CharField(_('文件编号'), max_length=200, null=True, blank=True)
    body_purpose = MDTextField(_('目的'), null=True, blank=True)
    body_scope = MDTextField(_('适用范围'), null=True, blank=True)
    body_definition = MDTextField(_('定义'), null=True, blank=True)
    body_duties = MDTextField(_('职责'), null=True, blank=True)
    body_program = MDTextField(_('程序'), null=True, blank=True)
    body_related_docs = MDTextField(_('相关文件和记录'), null=True, blank=True)
    document_level = models.ForeignKey(
        DocumentLevel,
        on_delete=models.SET_NULL,
        null=True,
        verbose_name=_('文件等级')
    )
    document_type = models.ForeignKey(
        DocType,
        on_delete=models.SET_NULL,
        null=True,
        verbose_name=_('文件类型')
    )
    company = models.ForeignKey(
        DocCompanyName,
        on_delete=models.SET_NULL,
        null=True,
        verbose_name=_('公司名称')
    )
    department = models.ForeignKey(
        DocDepartmentName,
        on_delete=models.SET_NULL,
        null=True,
        verbose_name=_('部门名称')
    )
    version_code = models.ForeignKey(
        VersionCode,
        on_delete=models.SET_NULL,
        null=True,
        verbose_name=_('版本编号')
    )
    published_at = models.DateField(_('发布日期'), null=True)
    current_active = models.BooleanField(_('当前在用'), default=True)
    file = models.FileField(_('SOP文件'), null=True, blank=True, upload_to='sop/')
    created_by = models.ForeignKey(
        'oa.WxUser',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name=_('创建人'),
        related_name='sop_document_created_by',
    )
    updated_by = models.ForeignKey(
        'oa.WxUser',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name=_('更新人'),
        related_name='sop_document_updated_by',
    )
    datetime_created = models.DateTimeField(_('创建时间'), auto_now_add=True)
    datetime_updated = models.DateTimeField(_('更新时间'), auto_now=True)

    objects = models.Manager()

    class Meta:
        ordering = ['document_type', 'code']
        verbose_name = _('文件')
        verbose_name_plural = _('文件')

    def __str__(self):
        return "{0}-{1}".format(self.code, self.name)

    def get_absolute_url(self):
        if self.pk:
            return reverse('sop:sop_doc', args=[self.pk])
        else:
            return reverse('sop:sop_docs')

    @mark_safe
    def link(self):
        return '<a href="{0}" target="_blank">链接</a>'.format(self.get_absolute_url())

    link.short_description = _('链接')
